﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ThinkDotNetEngine.Bots.Ammos;

namespace ThinkDotNetEngineDrawer.Bots.Ammos
{
    public class TextureAmmoDrawer:AmmoDrawerBase
    {
        private VertexBuffer _vertexBuffer;
        private IndexBuffer _indexBuffer;
        private BasicEffect _effect;
  

        #region Override

        public TextureAmmoDrawer(Game game, Type supportableAmmoType) : base(game, supportableAmmoType)
        {
        }

        public override void Initialize()
        {
            InitVertexBuffer();
            InitIndexBuffer();
            _effect = new BasicEffect(Game.GraphicsDevice);
            base.Initialize();
        }
        public override void DrawEffects(GameTime gameTime, AmmoBase ammo)
        {
            Game.GraphicsDevice.Indices = _indexBuffer;
            Game.GraphicsDevice.SetVertexBuffer(_vertexBuffer);
            Game.GraphicsDevice.BlendState = BlendState.AlphaBlend;

            _effect.Texture = ((IAmmoTexturable) ammo).AmmoTexture;
            _effect.TextureEnabled = true;            
            _effect.World = Matrix.CreateBillboard(ammo.CurrentPosition, World.Camera.CameraPosition, Vector3.Up, null);
            _effect.Projection = World.ProjMatrix;
            _effect.View = World.ViewMatrix;
            foreach (EffectPass pass in _effect.Techniques[0].Passes)
            {
                pass.Apply();
                Game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);           
            } 
        }

        #endregion

        #region Events

        #endregion

        #region Private methods

        private void InitVertexBuffer()
        {
            _vertexBuffer = new VertexBuffer(Game.GraphicsDevice,VertexPositionNormalTexture.VertexDeclaration, 4,
                                             BufferUsage.WriteOnly);

            VertexPositionNormalTexture pos1 = new VertexPositionNormalTexture();
            VertexPositionNormalTexture pos2 = new VertexPositionNormalTexture();
            VertexPositionNormalTexture pos3 = new VertexPositionNormalTexture();
            VertexPositionNormalTexture pos4 = new VertexPositionNormalTexture();

            const float size = 5;

            pos1.Position = new Vector3(-size / 2, -size / 2, 0);
            pos1.TextureCoordinate = new Vector2(0, 0);

            pos2.Position = new Vector3(size / 2, -size / 2, 0);
            pos2.TextureCoordinate = new Vector2(1, 0);

            pos3.Position = new Vector3(size / 2, size / 2, 0);
            pos3.TextureCoordinate = new Vector2(1, 1);

            pos4.Position = new Vector3(-size / 2, size / 2, 0);
            pos4.TextureCoordinate = new Vector2(0, 1);

            _vertexBuffer.SetData(new[] { pos1, pos2, pos3, pos4 });
        }

        private void InitIndexBuffer()
        {
            _indexBuffer = new IndexBuffer(Game.GraphicsDevice, typeof(short), 6, BufferUsage.WriteOnly);
            _indexBuffer.SetData(new short[] { 0, 1, 2, 0, 2, 3 });
        }

        #endregion
    }
}
